import { log } from '@main/core/logger';
import { ThemeMode } from '@shared/types.d';

import { config } from '../../core/config';
import windowManager from '../../core/window';

/**
 * 设置主题
 */
export function setTheme(theme: ThemeMode) {
  log.info('Setting theme to', theme);
  config.setTheme(theme);
}

/**
 * 获取主题
 */
export function getTheme() {
  log.info('Getting theme');
  return config.getTheme();
}

/**
 * 保存常规设置
 */
export function saveGeneralSettings(settings: {
  autoStart?: boolean;
  startToTray?: boolean;
  closeToTray?: boolean;
  language?: string;
  autoUpdate?: boolean;
  betaUpdates?: boolean;
  memoryLimit?: number;
  hardwareAcceleration?: boolean;
  showTrayIcon?: boolean;
  soundEffects?: boolean;
}): boolean {
  try {
    log.info('保存常规设置:', settings);
    config.updateGeneralSettings(settings);
    return true;
  } catch (error) {
    log.error('保存常规设置失败:', error);
    return false;
  }
}

/**
 * 获取常规设置
 */
export function getGeneralSettings(): {
  autoStart: boolean;
  startToTray: boolean;
  closeToTray: boolean;
  language: string;
  autoUpdate: boolean;
  betaUpdates: boolean;
  memoryLimit: number;
  hardwareAcceleration: boolean;
  showTrayIcon: boolean;
  soundEffects: boolean;
} {
  try {
    return {
      autoStart: config.getAutoStart(),
      startToTray: config.getStartToTray(),
      closeToTray: config.getCloseToTray(),
      language: config.getLanguage(),
      autoUpdate: config.getAutoUpdate(),
      betaUpdates: config.getBetaUpdates(),
      memoryLimit: config.getMemoryLimit(),
      hardwareAcceleration: config.getHardwareAcceleration(),
      showTrayIcon: config.getShowTrayIcon(),
      soundEffects: config.getSoundEffects()
    };
  } catch (error) {
    log.error('获取常规设置失败:', error);
    // 返回默认值
    return {
      autoStart: false,
      startToTray: false,
      closeToTray: true,
      language: 'zh-CN',
      autoUpdate: true,
      betaUpdates: false,
      memoryLimit: 2048,
      hardwareAcceleration: true,
      showTrayIcon: true,
      soundEffects: true
    };
  }
}

/**
 * 最小化窗口
 */
export function minimizeWindow(): boolean {
  try {
    windowManager.minimize();
    return true;
  } catch (error) {
    console.error('Failed to minimize window:', error);
    return false;
  }
}

/**
 * 最大化/还原窗口
 */
export function maximizeWindow(): boolean {
  try {
    windowManager.maximize();
    return true;
  } catch (error) {
    console.error('Failed to maximize window:', error);
    return false;
  }
}

/**
 * 关闭窗口
 */
export function closeWindow(): boolean {
  try {
    windowManager.close();
    return true;
  } catch (error) {
    console.error('Failed to close window:', error);
    return false;
  }
}

/**
 * 恢复窗口
 */
export function restoreWindow(): boolean {
  try {
    windowManager.restore();
    return true;
  } catch (error) {
    console.error('Failed to restore window:', error);
    return false;
  }
}

/**
 * 检查窗口是否最大化
 */
export function isWindowMaximized(): boolean {
  return windowManager.isMaximized();
}

/**
 * 检查窗口是否最小化
 */
export function isWindowMinimized(): boolean {
  return windowManager.isMinimized();
}

/**
 * 设置窗口是否置顶
 */
export function setWindowAlwaysOnTop(alwaysOnTop: boolean): boolean {
  try {
    config.setAlwaysOnTop(alwaysOnTop);
    return true;
  } catch (error) {
    console.error('Failed to set window always on top:', error);
    return false;
  }
}

/**
 * 检查窗口是否置顶
 */
export function isWindowAlwaysOnTop(): boolean {
  return config.getAlwaysOnTop();
}

/**
 * 获取窗口大小
 */
export function getWindowSize(): { width: number; height: number } {
  return windowManager.getWindowSize();
}

/**
 * 设置窗口大小
 */
export function setWindowSize(width: number, height: number, animate?: boolean): boolean {
  try {
    windowManager.setWindowSize(width, height, animate);
    return true;
  } catch (error) {
    console.error('Failed to set window size:', error);
    return false;
  }
}

/**
 * 获取窗口位置和大小
 */
export function getWindowBounds(): { x: number; y: number; width: number; height: number } {
  return windowManager.getWindowBounds();
}

/**
 * 设置窗口位置和大小
 */
export function setWindowBounds(
  bounds: { x?: number; y?: number; width?: number; height?: number },
  animate?: boolean
): boolean {
  try {
    windowManager.setWindowBounds(bounds, animate);
    return true;
  } catch (error) {
    console.error('Failed to set window bounds:', error);
    return false;
  }
}
